package com.zhao.flyweightFactory;

import com.zhao.concreteFlyweight.IBox;
import com.zhao.concreteFlyweight.LBox;
import com.zhao.concreteFlyweight.OBox;
import com.zhao.flyweight.AbstractBox;

import java.util.HashMap;
import java.util.Map;

public class BoxFactory {

    private Map<String, AbstractBox> abstractBoxMap = new HashMap<String, AbstractBox>();

    private BoxFactory() {
        abstractBoxMap.put("I", new IBox());
        abstractBoxMap.put("L", new LBox());
        abstractBoxMap.put("O", new OBox());
    }

    private static BoxFactory boxFactory;

    public static BoxFactory getInstance() {
        if (boxFactory == null) {
            synchronized (BoxFactory.class) {
                if (boxFactory == null) {
                    boxFactory = new BoxFactory();
                }
            }
        }
        return boxFactory;
    }

    public AbstractBox getBox(String shape) {
        return abstractBoxMap.get(shape.toUpperCase());
    }
}
